//
//  MainView.swift
//  GomroStore
//
//  Created by mac on 2020/11/20.
//

import UIKit

class MainView: BaseView {

    private var viewModel:MainViewModel?

    
    //MARK: - override
    override init(ViewModel vm: ViewModelProtocol) {
        self.viewModel = vm as? MainViewModel
        super.init(ViewModel: vm)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func setupViews() {
        self.addSubview(self.listTableView)
        
        //MARK:下拉刷新
        //self.listTableView.mj_header = K_APP_REFRESH_HEADER(refreshBlock: {[unowned self] in
            //...
        //})

        //MARK:上拉加载更多
        //self.listTableView.mj_footer = K_APP_REFRESH_FOOTER(refreshBlock: {[unowned self] in
           //...
        //})
        
        //self.listTableView.mj_header.endRefreshingCompletionBlock = { [unowned self] in
            //self.setEmptyView()
        //}
        
        //self.listTableView.mj_footer.isAutomaticallyChangeAlpha = true
        //self.listTableView.mj_header.beginRefreshing()
        
        setNeedsUpdateConstraints()
    }
    
    override func bindViewModel() {
        /*//刷新UI
        self.viewModel?.refreshUI.take(until: self.rac_willDeallocSignal())?.subscribeNext({ [unowned self] (_input:Any?) in
            guard let self = self else { return }
            self.refreshUI(input: _input)
        })
        
        self.viewModel?.loadDataCommand.executionSignals.switchToLatest()?.subscribeNext({ [unowned self] (_input:Any?) in
            guard let self = self else { return }
            self.refreshUI(input: _input)
        })
        
        //关闭动画
        self.viewModel?.stoploadingAnimation.take(until: self.rac_willDeallocSignal())?.subscribeNext({[unowned self] (input:Any?) in
            guard let self = self else { return }
            guard let (isLoadMore,hasNextPage) = input as? (Bool,Bool) else { return }
            
            //结束刷新
            if isLoadMore {
                if hasNextPage == false {
                    self.listTableView.mj_footer?.endRefreshingWithNoMoreData()
                }
                else{
                    self.listTableView.mj_footer?.resetNoMoreData()
                    self.listTableView.mj_footer?.endRefreshing()
                }
            }
            else{
                self.listTableView.mj_header?.endRefreshing()
                self.listTableView.mj_footer?.resetNoMoreData()

                if hasNextPage == false {
                    self.listTableView.mj_footer?.endRefreshingWithNoMoreData()
                }
            }
            
            if AppDelegate.app().isNetWork() {
                self.listTableView.reloadData()
            }
            
            self.setEmptyView()
        })
 */
    }
    
    
    //MARK: - lazy load
    private lazy var listTableView:UITableView = {
        let _tb = UITableView.init(frame: CGRect.zero,
                                   style: UITableView.Style.plain)
        _tb.backgroundColor = UIColor.clear
        _tb.isScrollEnabled = true
        
        //适配平板
        _tb.cellLayoutMarginsFollowReadableWidth = false
        
        _tb.showsVerticalScrollIndicator = false
        _tb.showsHorizontalScrollIndicator = false
        
        //分割线
        _tb.separatorStyle = .none
        _tb.separatorColor = K_APP_SPLIT_LINE_COLOR
        
        //行选中
        _tb.allowsSelection = true
        _tb.allowsMultipleSelection = false
        
        //表头、表尾
        _tb.tableHeaderView = UIView.init(frame: .zero)
        _tb.tableFooterView = UIView.init(frame: .zero)
        
        //_tb.delegate = self
        //_tb.dataSource = self
        
        //防止顶部空白
        if #available(iOS 11.0, *) {
            _tb.contentInsetAdjustmentBehavior = UIScrollView.ContentInsetAdjustmentBehavior.never
        }
        
        //注册
//        _tb.register(OrderListTableViewCell.classForCoder(),
//                     forCellReuseIdentifier: OrderListTableViewCell.cell_identify)
//        _tb.register(OrderListTableViewCell2.classForCoder(),
//                     forCellReuseIdentifier: OrderListTableViewCell2.cell_identify)
        
        return _tb
    }()
    
    private lazy var emptyView:EmptyView = {
        let rect = CGRect.init(x: (K_APP_WIDTH - EmptyView.k_width) * 0.5,
                               y: (K_APP_HEIGHT - K_APP_NAVIGATION_BAR_HEIGHT - EmptyView.k_height) * 0.5,
                               width: EmptyView.k_width, height: EmptyView.k_height)
        let _em = EmptyView.init(frame: rect)
        _em.defaultType = .no_order
        _em.ctype = .no_order
        _em.isHidden = true
        
        _em.noNetworkRefreshBlock = {[weak self] in
            guard let self = self else { return }
            
            if !AppDelegate.app().isNetWork() {
                print("无网刷新")
                self.listTableView.mj_header.beginRefreshing()
            }
        }
        
        return _em
    }()
}


//MARK: -
extension MainView {
    
//    private func refreshUI(input:Any?){
//        guard let (isLoadMore,listDatas,removeAll) = input as? (Bool,[OrderModel]?,Bool?) else { return }
//
//        if !isLoadMore {
//            self.arrListData = listDatas
//        }
//        else{
//            if self.arrListData == nil {
//                self.arrListData = [OrderModel]()
//            }
//
//            if listDatas != nil {
//                self.arrListData = self.arrListData! + listDatas!
//            }
//        }
//
//        if removeAll == true {
//            self.arrListData = nil
//        }
//        self.listTableView.reloadData()
//        self.setEmptyView()
//    }
//
//    private func setEmptyView(){
//        if self.arrListData != nil && self.arrListData!.count > 0 {
//            self.emptyView.isHidden = true
//        }
//        else{
//            self.emptyView.isHidden = false
//        }
//    }
    
}
